/**   
 * @Title: DaletouMainThread.java 
 * @Package: com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description: 大乐透主界面刷新 
 * @Author: Miko baoz@500wan.com   
 * @Date: 2011-7-5 下午03:01:12  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import java.util.Hashtable;

import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.esun.model.DaletouInfo;
import com.esun.rabbit2.communicaters.mainthread.DaletouTermInfoCommunicater;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.TempData;

/**
 * @ClassName: DaletouMainThread
 * @Description: 大乐透主界面刷新
 * @Author: Miko baoz@500wan.com
 * @Date: 2011-7-5 下午03:01:12
 * 
 */
public class DaletouMainThread {

	private TextView tvTerm;
	private TextView tvTime;
	private ProgressBar pb;

	private Handler handler;

	private static final int GET_TERM_SUCCESS = 10001;
	private static final int GET_TERM_FAILED = 10002;

	private Hashtable<String, String> htTermInfo = new Hashtable<String, String>();

	private DaletouTermInfoCommunicater communicater = new DaletouTermInfoCommunicater();

	private MyTimeLeftTimeCount ticker;

	private boolean isRunning = false;
	private boolean isCanBuy = false;

	public DaletouMainThread(TextView tvTerm, TextView tvTime, ProgressBar pb) {
		this.tvTerm = tvTerm;
		this.tvTime = tvTime;
		this.pb = pb;

		initHandler();
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progressGone();

				switch (msg.what) {
				case GET_TERM_SUCCESS:

					isCanBuy = true;
					isRunning = false;

					String strShow = "距离" + htTermInfo.get(TempData.EXPECT) + "期截止";
					tvTerm.setText(strShow);
					DaletouInfo.getDaletouInfo().setTermNum(htTermInfo.get(TempData.EXPECT));
					// ShuangseqiuInfo.getShuangseqiuInfo().setTermNum(htTermInfo.get(TempData.EXPECT));
					resetTicker();

					break;

				case GET_TERM_FAILED:
					isCanBuy = false;
					isRunning = false;
					tvTerm.setText("获取期号信息失败");
					break;

				default:
					break;
				}
			}
		};
	}

	public void refresh() {

		if (!isRunning) {

			progressVisible();

			isCanBuy = false;

			tvTerm.setText("期号获取中...");
			tvTime.setText("_天_小时_分_秒");

			new Thread() {

				@Override
				public void run() {
					super.run();
					isRunning = true;
					if (!doGetTermInfo()) {
						try {
							Thread.sleep(3000);
						} catch (InterruptedException e) {
						}
					}
				}
			}.start();
		}
	}

	private boolean doGetTermInfo() {

		if (communicater.getTermInfo()) {

			htTermInfo = communicater.getCurrentTerm();

			Message msg = handler.obtainMessage(GET_TERM_SUCCESS);
			handler.sendMessage(msg);

			return true;
		} else {

			Message msg = handler.obtainMessage(GET_TERM_FAILED);
			handler.sendMessage(msg);
			return false;
		}
	}

	private class MyTimeLeftTimeCount extends CountDownTimer {

		String strMin;
		String strSec;
		String strHour;
		String strDay;
		long time;

		public MyTimeLeftTimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {

			time = millisUntilFinished / 1000;

			// 剩余天数
			strDay = String.valueOf(time / 86400);

			time = time % 86400;

			// 剩余小时
			strHour = String.valueOf(time / 3600);
			if (strHour.length() < 2) {
				strHour = TempData.NUM0 + strHour;
			}

			time = time % 3600;

			// 剩余分钟数
			strMin = String.valueOf(time / 60);
			if (strMin.length() < 2) {
				strMin = TempData.NUM0 + strMin;
			}

			// 剩余秒数
			strSec = String.valueOf(time % 60);
			if (strSec.length() < 2) {
				strSec = TempData.NUM0 + strSec;
			}

			tvTime.setText(strDay + "天" + strHour + "小时" + strMin + "分" + strSec + "秒");
		}

		@Override
		public void onFinish() {

			tvTerm.setText("第" + htTermInfo.get(TempData.EXPECT) + "期已截止");
			tvTime.setText("预售" + communicater.getNexttTerm() + "期");
			DaletouInfo.getDaletouInfo().setTermNum(communicater.getNexttTerm());

		}
	}

	private void resetTicker() {
		if (ticker != null) {
			ticker.cancel();
		}

		ticker = new MyTimeLeftTimeCount(Long.parseLong(htTermInfo.get(TempData.LEFT_TIME)) * 1000, 1000);
		ticker.start();
	}

	public boolean isCanbuy() {
		return this.isCanBuy;
	}

	private void progressVisible() {
		pb.setVisibility(View.VISIBLE);
	}

	private void progressGone() {
		pb.setVisibility(View.GONE);
	}

	// private void progressInvisible() {
	// pb.setVisibility(View.INVISIBLE);
	// }
}
